<!DOCTYPE html >
<html xmlns="http://www.w3.org/1999/xhtml" lang="en">      <!--声明Thymeleaf命名空间-->
<head>
    <title>缓存管理</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <link rel="stylesheet" href="/css/bootstrap.min.css" ></link>
</head>
<body>

<nav class="navbar navbar-default">
    <div class="container-fluid">
        <!-- Brand and toggle get grouped for better mobile display -->
        <div class="navbar-header">
            <a class="navbar-brand" href="/index.html">系统后台管理</a>
        </div>

        <!-- Collect the nav links, forms, and other content for toggling -->
        <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
            <ul class="nav navbar-nav ">
                <li ><a href="/admin/role.html">角色管理</a></li>
                <li ><a href="/admin/menu.html">菜单管理</a></li>
                <li><a href="/admin/apis.html">API管理</a></li>
                <li class="active"><a href="/admin/cache.html">缓存管理</a></li>
            </ul>

        </div><!-- /.navbar-collapse -->

    </div><!-- /.container-fluid -->
</nav>
<div class="container-fluid">

    <ol class="breadcrumb">
        <li><a href="#">首页</a></li>
        <li><a href="#">权限管理</a></li>
        <li class="active">缓存管理</li>
    </ol>
    <div class="panel panel-default">
        <div class="panel-body">
            <h1>缓存管理</h1>
            <h3>刷新没有依赖的缓存会把它的子缓存一起刷新掉</h3>
            <a class="btn btn-default" id="tb"  style="margin-bottom: 10px;" role="button">新增</a>
            <a class="btn btn-default" id="shuaxin"  style="margin-bottom: 10px;" role="button">刷新全部缓存</a>
            <table id="table" class="table table-bordered table-striped">
                <tr>
                    <th>缓存名称</th>
                    <th>缓存备注</th>
                    <th>依赖的父级缓存名称</th>
                    <th>上次缓存时间</th>
                    <th>类型</th>
                    <th>缓存时间(秒)</th>
                    <th>状态</th>
                    <th>操作</th>
                </tr>
            </table>
        </div>
    </div>
</div>

<!-- 模态框（Modal） -->
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
    <div class="modal-dialog" style="width: 880px;">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                <h4 class="modal-title" id="myModalLabel">新增修改缓存</h4>
            </div>
            <div class="modal-body">
                <input type="hidden" id="id">
                <form class="form-horizontal" id="cacheForm">
                    <div class="form-group">
                        <label for="name" class="col-sm-2 control-label">缓存名称</label>
                        <div class="col-sm-10">
                            <input type="text" class="form-control" name="name"  id="name" placeholder="缓存名称" required>
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="memo" class="col-sm-2 control-label">缓存备注</label>
                        <div class="col-sm-10">
                            <input type="text" class="form-control" name="memo"  id="memo" placeholder="缓存备注" >
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="parentName" class="col-sm-2 control-label">依赖的父级缓存名称</label>
                        <div class="col-sm-10">
                            <input type="text" class="form-control" name="parentName"  id="parentName" placeholder="依赖的父级缓存名称" >
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="type" class="col-sm-2 control-label">缓存类型</label>
                        <div class="col-sm-10">
                            <select  class="form-control" name="type"  id="type" placeholder="缓存类型" >
                                <option value="功能缓存">功能缓存</option>
                                <option value="首页缓存">首页缓存</option>
                                <option value="列表缓存">列表缓存</option>
                                <option value="工具缓存">工具类缓存</option>
                            </select>
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="cacheTime" class="col-sm-2 control-label">缓存时间(秒)</label>
                        <div class="col-sm-10">
                            <input type="number" class="form-control" name="cacheTime"  id="cacheTime" placeholder="缓存时间" required value="86400">
                        </div>
                    </div>
                </form>


            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
                <button type="button" id="saveMenu" class="btn btn-primary">保存</button>
            </div>
        </div><!-- /.modal-content -->
    </div><!-- /.modal -->
</div>

<!-- jQuery (Bootstrap 的所有 JavaScript 插件都依赖 jQuery，所以必须放在前边) -->
<script src="/js/jquery.min.js"></script>
<!-- 加载 Bootstrap 的所有 JavaScript 插件。你也可以根据需要只加载单个插件。 -->
<script src="/js/bootstrap.min.js"></script>
<script src="/js/bootstrapValidator.js"></script>
<script src="/js/auth.js"></script>
<script>
    $.ajax({
        type: 'GET',
        url: '/v1/cacheTables/list',
        dataType: 'json',
        success:function(data){
            if(data!=null&&data.data!=null&&data.code==200){
                var dataList=data.data;
                var html=$("#table").html();
                var tr="";
                for(var i=0;i<dataList.length;i++) {
                    tr += '<tr>';
                    tr += '<td>' + dataList[i].name;
                    tr += '</td>';
                    if (dataList[i].memo) {
                        tr += '<td>' + dataList[i].memo;
                        tr += '</td>';
                    } else {
                        tr += '<td> -- ';
                        tr += '</td>';
                    }
                    if (dataList[i].parentName) {
                        tr += '<td>' + dataList[i].parentName;
                        tr += '</td>';
                    } else {
                        tr += '<td> -- ';
                        tr += '</td>';
                    }
                    if (dataList[i].oldUpdateTime) {
                        tr += '<td>' + dataList[i].oldUpdateTime;
                        tr += '</td>';
                    } else {
                        tr += '<td> 未更新 ';
                        tr += '</td>';
                    }
                    if (dataList[i].type) {
                        tr += '<td>' + dataList[i].type;
                        tr += '</td>';
                    } else {
                        tr += '<td> -- ';
                        tr += '</td>';
                    }
                    if (dataList[i].cacheTime) {
                        tr += '<td>' + dataList[i].cacheTime;
                        tr += '</td>';
                    } else {
                        tr += '<td> 默认1天 ';
                        tr += '</td>';
                    }
                    if (dataList[i].deleteFlag) {
                        if (dataList[i].deleteFlag == 0){
                            tr += '<td>正常';
                        tr += '</td>';
                         } else {
                            tr += '<td> 不参与定时缓存 ';
                            tr += '</td>';
                         }
                    }else {
                        tr += '<td> -- ';
                        tr += '</td>';
                    }

                    tr+='<td><button class="btn btn-default" onclick="refreshCache(\''+dataList[i].id+'\')" type="button">更新缓存</button>';
                    tr+='<button class="btn btn-default" onclick="updateCache(\''+dataList[i].id+'\')" type="button">修改缓存信息</button>';
                    tr+='<button class="btn btn-default" onclick="clearCache(\''+dataList[i].id+'\')" type="button">清空缓存</button>';
                    tr+='<button class="btn btn-default" onclick="updateCacheFlag(\''+dataList[i].id+'\')" type="button">不参与定时缓存</button>';

                    tr+='<button class="btn btn-default" onclick="deleteCache(\''+dataList[i].id+'\')" type="button">彻底删除此缓存项</button>';
                    tr+='</td>';
                    tr+='</tr>';

                }
                html+=tr;
                $("#table").html(html);

            }
        }
    });
    $("#tb").click(function(){
        $("#id").val("");
        $("#name").val("");
        $("#memo").val("");
        $("#type").val("功能缓存");
        $("#cacheTime").val("86400");
        $("#parentName").val("");
        $('#myModal').modal('show');
    });
    $("#shuaxin").click(function(){
        $.ajax({
            type: 'GET',
            url: '/v1/cacheTables/refreshAll',
            dataType: 'json',
            success:function(data){
                if(data!=null&&data.code==200){
                    alert("刷新成功");
                }else{
                    alert("刷新失败"+data.message);
                }
            },
            error:function(jqXHR, textStatus, errorThrown){
                var json = JSON.parse(jqXHR.responseText);
                alert(json.message);
            }
        });
    });
    $('#cacheForm').bootstrapValidator();
    $("#saveMenu").click(function(){
        var form =$("#cacheForm");
        form.data("bootstrapValidator").validate(); // 手动验证
        var isValid = form.data("bootstrapValidator").isValid(); // 是否验证成功
        if(isValid){

        }else{
          //  alert("请检查")
            return;
        }
        var data={
            "id":$("#id").val(),
            "name": $("#name").val(),
            "memo":$("#memo").val(),
            "type":$("#type").val(),
            "cacheTime":$("#cacheTime").val(),
            "parentName":$("#parentName").val()
        };
        var mtype="POST";
        if($("#id").val()!=null&&$("#id").val()!=''){
            mtype="PUT";
        }

        $.ajax({
            type: mtype,
            url: '/v1/cacheTables',
            dataType: 'json',
            data:JSON.stringify(data),
            contentType: "application/json",
            success:function(data){
                if(data!=null&&data.code==200){
                    alert("保存成功");
                    $('#myModal').modal('hide');
                    location.reload();
                }

            },
            error:function(jqXHR, textStatus, errorThrown){
                var json = JSON.parse(jqXHR.responseText);
                alert(json.message);
            }
        });
    });
    function updateCache(id){
        $("#id").val("");
        $("#name").val("");
        $("#memo").val("");
        $("#type").val("功能缓存");
        $("#cacheTime").val("86400");
        $("#parentName").val("");
        $.ajax({
            type: 'GET',
            url: '/v1/cacheTables/'+id,
            dataType: 'json',
            success:function(data){
                if(data!=null&&data.data!=null&&data.code==200) {
                    $("#id").val(id);
                    $("#name").val(data.data.name);
                    $("#memo").val(data.data.memo);
                    $("#type").val(data.data.type);
                    $("#cacheTime").val(data.data.cacheTime);
                    $("#parentName").val(data.data.parentName);
                }
            },
            error:function(jqXHR, textStatus, errorThrown){
                var json = JSON.parse(jqXHR.responseText);
                alert(json.message);
            }
        });
        $('#myModal').modal('show')
    }
    function refreshCache(id){
        if(confirm("确定要刷新缓存吗？(刷新后得到最新的刷新结果并缓存)"))
        {
            $.ajax({
                type: 'GET',
                url: '/v1/cacheTables/refresh/'+id,
                dataType: 'json',
                success:function(data){
                    if(data!=null&&data.code==200) {
                        alert("刷新成功！")
                        location.reload();
                    }else{
                        alert("刷新失败！"+data.message)
                    }
                },
                error:function(jqXHR, textStatus, errorThrown){
                    var json = JSON.parse(jqXHR.responseText);
                    alert(json.message);
                }
            });
        }
    }

    function clearCache(id){
        if(confirm("确定要清空缓存吗？(清空后下次访问或定时缓存时继续缓存)"))
        {
            $.ajax({
                type: 'PUT',
                url: '/v1/cacheTables/clear/'+id,
                dataType: 'json',
                success:function(data){
                    if(data!=null&&data.code==200) {
                        alert("清空成功！")
                       //location.reload();
                    }else{
                        alert("清空失败！"+data.message)
                    }
                },
                error:function(jqXHR, textStatus, errorThrown){
                    var json = JSON.parse(jqXHR.responseText);
                    alert(json.message);
                }
            });
        }
    }
    function deleteCache(id){

        if(confirm("确定要删除此缓存吗？"))
        {
            $.ajax({
                type: 'DELETE',
                url: '/v1/cacheTables/'+id,
                dataType: 'json',
                success:function(data){
                    if(data!=null&&data.code==200) {
                        alert("删除成功！")
                        location.reload();
                    }else{
                        alert("删除失败！"+data.message)
                    }
                },
                error:function(jqXHR, textStatus, errorThrown){
                    var json = JSON.parse(jqXHR.responseText);
                    alert(json.message);
                }
            });
        }
    }
    function updateCacheFlag(id){

        if(confirm("确定要不参与定时缓存吗？"))
        {
            var data={
                "id":id,
                "deleteFlag":'1'
            };
            $.ajax({
                type: 'PUT',
                url: '/v1/cacheTables',
                dataType: 'json',
                data:JSON.stringify(data),
                contentType: "application/json",
                success:function(data){
                    if(data!=null&&data.code==200) {
                        alert("操作成功！")
                        location.reload();
                    }else{
                        alert("操作失败！"+data.message)
                    }
                },
                error:function(jqXHR, textStatus, errorThrown){
                    var json = JSON.parse(jqXHR.responseText);
                    alert(json.message);
                }
            });
        }
    }
</script>
</body>
</html>